import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/dashboard',
    },
    {
      path: '/',
      name: 'Home',
      component: HomeView,
      children: [
        {
          path: '/dashboard',
          name: 'dashboard',
          meta: {
            title: '系统首页',
          },
          component: () => import('../views/dashboard/DashboardView.vue'),
        },
        {
          path: '/userInfo',
          name: 'userInfo',
          meta: {
            title: '用户信息',
          },
          component: () => import('../views/user/UserInfo.vue'),
        },
        {
          path: '/userAdd',
          name: 'userAdd',
          meta: {
            title: '用户添加',
          },
          component: () => import('../views/user/UserAdd.vue'),
        },
        {
          path: '/articleInfo',
          name: 'articleInfo',
          meta: {
            title: '文章信息',
          },
          component: () => import('../views/articles/ArticleInfo.vue'),
        },
        {
          path: '/articleAdd',
          name: 'articleAdd',
          meta: {
            title: '文章添加',
          },
          component: () => import('../views/articles/ArticleAdd.vue'),
        },
      ],
    },
    {
      path: '/login',
      name: 'Login',
      meta: {
        title: '登录',
      },
      component: () => import('../views/login/LoginView.vue'),
    },
  ]
});

//引入全局路由守卫
router.beforeEach((to, from) => {
  if (to.path !== "/login") {
    if (!window.localStorage.getItem("userInfo")) {
      return {
        path: "/login",
      };
    }
  }
});

export default router
